// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Z [author] rhyme_lph
// q [email]  rhymelph@gmail.com
// b [github] https://github.com/rhymelph

import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/setting_entity.dart';
import 'package:finance_app/src/mixin/search_data_list_mixin.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/permission_utils.dart';

class SettingProvider extends BaseProvider<SettingEntity> with SearchDataListMixin {
  List _functionList = [
    if (((Platform.isAndroid && Config.settingApp.androidStatus == 1) || (Platform.isIOS && Config.settingApp.iosStatus == 1)) &&
        Config.myPermission?.canEdit == 0)
      {
        'title': S.current.subscription,
        'content': S.current.subscription_detail,
        'path': A.assetsImagesStyleCoin_1,
        'router': RouterGenProviders.subscriptionPage,
      },
    {
      'title': S.current.account_settings,
      'content': S.current.account_settings_detail,
      'path': A.assetsImagesStyleUser_1,
      'router': RouterGenProviders.accountSettingsPage,
    },
    if (Config.hasPermission(PermissionUtils.companySettings))
      {
        'title': S.current.company_settings,
        'content': S.current.company_profile_detail,
        'path': A.assetsImagesStyleBusiness_and_trade_1,
        'router': RouterGenProviders.companyProfilePage,
      },

    if (Config.hasPermission(PermissionUtils.companySettings))
      {
        'title': S.current.gst,
        'content': S.current.gst_detail,
        'path': A.assetsImagesStyleTax_1_1,
        'router': RouterGenProviders.gstPage,
      },
    if (Config.hasPermission(PermissionUtils.chartOfAccounts))
      {
        'title': S.current.chart_of_accounts,
        'content': S.current.chart_of_accounts_detail,
        'path': A.assetsImagesStyleBook_1,
        'router': RouterGenProviders.chartOfAccountsPage,
      },
    // {
    //   'title': S.current.preferences,
    //   'content': S.current.preferences_detail,
    //   'path': A.assetsImagesStyleSettings_2,
    //   'router': RouterGenProviders.preferencesPage,
    // }
  ];

  List get functionList => _functionList;

  @override
  void initState() {
    super.initState();
    _getCanUploadFlag();
  }

  @override
  Future get dataFuture => null;

  void _getCanUploadFlag() async {
    await post(BaseConfig.httpBase.get('/api/init/import_data/check', {}), autoHandler: AutoHandler.Toast, onSuccess: (result) {
      var datas = result.data['data'];
      SettingEntity entity = SettingEntity.fromJson(datas);
      if (entity.can_init_import_data == true) {
        functionList.insert(2, {
          'title': S.current.data_import,
          'content': S.current.contact_ar_ap_import,
          'path': A.assetsDataImport,
          'router': RouterGenProviders.settingDataImport,
        });
        update();
      }
    });
  }

  @override
  void searchRefreshData(data) {}

  void onNavFunction(String router) {
    unFocus();
    Routes.navigateTo(router);
  }
}
